package com.lg.partition15;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/4/6 21:54
 */
public class P1439模板_最长公共子序列_线性dp {

    private static final int N = 1010;
    private static int[] p = new int[N], q = new int[N];
    private static int[][] f = new int[N][N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) p[i] = Integer.parseInt(ss[i - 1]);
        ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) q[i] = Integer.parseInt(ss[i - 1]);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (p[i] == q[j]) f[i][j] = f[i - 1][j - 1] + 1;
                else f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
            }
        }
        writer.write(f[n][n] + "\n");
        writer.flush();
    }
}
